/*
	SimpleMan - Simple Download Manager
	Copyright (C) 2007 Roque Pinel, Rio de Janeiro
	Email simplemandownloader@gmail.com
*/
/*
	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include <gtkmm/stock.h>

#include "sm_new.h"

#define SIZE_X 500
#define SIZE_Y 190


SM_New::SM_New ( Gtk::Window &win, const std::string &path )
	: Gtk::Dialog ( "New Task", win, false, false ),
	  sm_boxFrameFile( false, 0 ),
	  sm_boxURL( false, 0 ),
	  sm_boxDir( false, 0 ),
	  sm_boxName( false, 0 ),
	  sm_boxRadio( false, 0 ),
	  sm_LabelURL( "  URL:         ", false ),
	  sm_LabelDir( "  Save To:    ", false ),
	  sm_LabelName( "  Name:       ", false ),
	  sm_Button_Folder ( "..." ),
	  sm_RadioButtonOn( "On" ),
	  sm_RadioButtonOff( "Off" ),
	  sm_group ( sm_RadioButtonOn.get_group() ),
	  sm_AlignmentRadio( Gtk::ALIGN_CENTER, Gtk::ALIGN_CENTER, 0.0, 0.0 )
{
	set_size_request( SIZE_X, SIZE_Y );
	set_resizable ( false );
	
	sm_newBox = get_vbox();
	sm_newBox->set_homogeneous( false );
	sm_newBox->set_spacing( 0 );
	
	sm_FrameFile.set_label( "File Options" );
	sm_FrameFile.set_label_align( Gtk::ALIGN_CENTER, Gtk::ALIGN_TOP );
	sm_FrameFile.set_shadow_type( Gtk::SHADOW_ETCHED_OUT );	
	sm_newBox->pack_start( sm_FrameFile, Gtk::PACK_SHRINK, 3 );
	
	sm_EntryURL.set_width_chars( 50 );
	sm_EntryURL.set_activates_default( true );
	sm_EntryURL.signal_changed().connect( sigc::mem_fun( *this, &SM_New::on_changed_url ) );
	sm_boxURL.pack_start( sm_LabelURL, Gtk::PACK_SHRINK, 0 );
	sm_boxURL.pack_start( sm_EntryURL, Gtk::PACK_SHRINK, 0 );
	
	sm_EntryDir.set_text( path );
	sm_EntryDir.set_width_chars( 47 );
	sm_EntryDir.set_activates_default( true );
	sm_Button_Folder.signal_clicked().connect( sigc::mem_fun( *this, &SM_New::on_button_folder_new ) );
	sm_boxDir.pack_start( sm_LabelDir, Gtk::PACK_SHRINK, 0 );
	sm_boxDir.pack_start( sm_EntryDir, Gtk::PACK_SHRINK, 0 );
	sm_boxDir.pack_start( sm_Button_Folder, Gtk::PACK_SHRINK, 0 );
	
	sm_EntryName.set_width_chars( 50 );
	sm_EntryName.set_activates_default( true );
	sm_boxName.pack_start( sm_LabelName, Gtk::PACK_SHRINK, 0 );
	sm_boxName.pack_start( sm_EntryName, Gtk::PACK_SHRINK, 0 );
	
	sm_FrameFile.add( sm_boxFrameFile );
	sm_boxFrameFile.pack_start( sm_boxURL, Gtk::PACK_SHRINK, 0 );
	sm_boxFrameFile.pack_start( sm_boxDir, Gtk::PACK_SHRINK, 0 );
	sm_boxFrameFile.pack_start( sm_boxName, Gtk::PACK_SHRINK, 0 );
		
	sm_FrameStartOptions.set_label( "Starting now?" );
	sm_FrameStartOptions.set_label_align( Gtk::ALIGN_CENTER, Gtk::ALIGN_TOP );
	sm_FrameStartOptions.set_shadow_type( Gtk::SHADOW_ETCHED_OUT );	
	sm_newBox->pack_start( sm_FrameStartOptions, Gtk::PACK_SHRINK, 3 );

	sm_RadioButtonOff.set_group( sm_group );
	sm_AlignmentRadio.add( sm_boxRadio );
	sm_boxRadio.pack_start( sm_RadioButtonOn, Gtk::PACK_SHRINK, 0 );
	sm_boxRadio.pack_start( sm_RadioButtonOff, Gtk::PACK_SHRINK, 0 );
	sm_FrameStartOptions.add( sm_AlignmentRadio );

	add_button( Gtk::Stock::CANCEL, 0 );
	add_button( Gtk::Stock::OK, 1 );

	set_default_response( 1 );	
	activate_default();

	show_all_children();	
}


	/* event functions */

void SM_New::on_changed_url ( void )
{
	std::string url = sm_EntryURL.get_text();

	unsigned int posA = url.find( "://" );

	if( posA != std::string::npos )
	{
		posA += 3;
		unsigned int posB = url.find_last_of( "/" );

		if ( posB != std::string::npos && posB > posA )
		{
			std::string name = url.substr( posB+1, url.length() );
			sm_EntryName.set_text( name );
		}
	}	
}

void SM_New::on_button_folder_new ( void )
{
	Gtk::FileChooserDialog sm_folderDialog( "Please choose a folder", Gtk::FILE_CHOOSER_ACTION_SELECT_FOLDER );
	sm_folderDialog.set_transient_for( *this );

		/* add response buttons the the dialog */

	sm_folderDialog.add_button( Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL );
	sm_folderDialog.add_button( "Select", Gtk::RESPONSE_OK );

	int result = sm_folderDialog.run();

		/* handle the response */

	if( result == Gtk::RESPONSE_OK )
		sm_EntryDir.set_text( sm_folderDialog.get_filename() );
}


	/* set functions */

void SM_New::set_url ( const std::string &url )
{
	sm_EntryURL.set_text( url );
	on_changed_url();
}

void SM_New::set_start ( const bool & startNow )
{
	if( startNow )
		sm_RadioButtonOn.set_active();
	else
		sm_RadioButtonOff.set_active();		
}


	/* get functions */

const std::string SM_New::get_url ( void ) const
{
	return sm_EntryURL.get_text();
}

const std::string SM_New::get_dir ( void ) const
{
	return sm_EntryDir.get_text();
}

const std::string SM_New::get_name ( void ) const
{
	return sm_EntryName.get_text();
}

const bool SM_New::get_start ( void ) const
{
	return sm_RadioButtonOn.get_active();
}


/* EOF */

